import { call, put, takeEvery} from 'redux-saga/effects'
import { fetchJobListAPI, fetchJobDetailAPI, updateJobStatusAPI, updateSubmitStatusAPI, updateJobCategoryAPI } from './apis.js'
import { putJobList, putJobDetail } from './reducers.js'
import { FETCH_JOB_LIST, FETCH_JOB_DETAIL, UPDATE_JOB_STATUS, UPDATE_SUBMIT_STATUS, UPDATE_JOB_CATEGORY } from './actions.js'


export function* fetchJobList(action) {
  const result = yield fetchJobListAPI()
  // console.log('result:', result)
  yield put(putJobList(result))
}

function* fetchJobDetail(action) {
  const { payload } = action
  console.log('fetchJobDetail: ', payload)
  const result = yield fetchJobDetailAPI(payload)
  yield put(putJobDetail(result))
}

function* updateJobStatus(action) {
  const { payload } = action
  console.log('updateJobStatus: ', payload)
  const result = yield updateJobStatusAPI(payload)
  yield put(putJobList(result))
}

function* updateSubmitStatus(action) {
  console.log('++++updateSubmitStatus')
  const { payload } = action
  const result = yield updateSubmitStatusAPI(payload)
  yield put(putJobList(result))
}

function* updateJobCategory(action) {
  console.log('++++updateJobCategory')
  const { payload } = action
  const result = yield updateJobCategoryAPI(payload)
  console.log('updateJobCategory result ', result)
}


export function* rootSaga() {
  yield takeEvery(FETCH_JOB_LIST, fetchJobList)
  yield takeEvery(FETCH_JOB_DETAIL, fetchJobDetail)
  yield takeEvery(UPDATE_JOB_STATUS, updateJobStatus)
  yield takeEvery(UPDATE_SUBMIT_STATUS, updateSubmitStatus)
  yield takeEvery(UPDATE_JOB_CATEGORY, updateJobCategory)

  
}


